import { createRouter, createWebHistory } from 'vue-router'
import ChatTerminal from '../components/ChatTerminal.vue'
import ChatCall from '../components/ChatCall.vue'
import { checkIpRestriction } from '../utils/api'
import TableStructure from '../components/TableStructure.vue'
import BusinessTerms from '../components/BusinessTerms.vue'

const routes = [
  {
    path: '/',
    redirect: '/ai/chat'
  },
  {
    path: '/ai/chat',
    name: 'AIChat',
    component: ChatTerminal
  },
  {
    path: '/ai/chat/tools',
    name: 'AIChatTools',
    component: ChatCall
  },
  {
    path: '/ai/chat/all',
    name: 'chatTestData',
    component: () => import('../components/ChatTestData.vue'),
    meta: { requiresIpCheck: true }
  },
  {
    path: '/table-structure',
    name: 'TableStructure',
    component: TableStructure
  },
  {
    path: '/business-terms',
    name: 'BusinessTerms',
    component: BusinessTerms
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach(async (to, from, next) => {
  if (to.meta.requiresIpCheck) {
    const { restricted } = await checkIpRestriction()
    if (restricted) {
      next({ name: 'Home' })
      return
    }
  }
  next()
})

export default router